package com.css.fxfzysx.modules.vaolEngineering.controller;

import cn.hutool.core.io.FileUtil;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.modules.vaolEngineering.entity.CtVulnerabilityEntity;
import com.css.fxfzysx.modules.vaolEngineering.param.update.TsParam;
import com.css.fxfzysx.modules.vaolEngineering.service.TsVulnerabilityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 变电站
 */
@RestController
@RequestMapping("tsVulnerability")
@Slf4j
public class TsVulnerabilityController {

    @Autowired
    private TsVulnerabilityService tsVulnerabilityService;

    /**
     * 变电站易损性计算和保存
     */
    @GetMapping("caTsVulnerability")
    public RestResponse caTsVulnerability(String versionId,String tableId){
        try {
            return tsVulnerabilityService.caTsVulnerability(versionId,tableId);
        } catch (Exception e) {
            log.error("get caTsVulnerability error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

    /**
     * 变电站排序
     */
    @PostMapping("groupTsVulnerability")
    public RestResponse groupTsVulnerability(@RequestBody List<CtVulnerabilityEntity> ctVulnerabilityList) {
        return tsVulnerabilityService.groupTsVulnerability(ctVulnerabilityList);
    }

    /**
     * 变电站导入
     */
    @PostMapping("importTsVulnerability")
    public RestResponse importInterfaceWelding(@RequestParam MultipartFile file){
        if (!FileUtil.getSuffix(file.getOriginalFilename()).equals("xls") && !FileUtil.getSuffix(file.getOriginalFilename()).equals("xlsx")) {
            return RestResponse.fail("文件格式不支持!");
        }
        return tsVulnerabilityService.importInterfaceWelding(file);
    }

    /**
     * 数据验证
     */
    @GetMapping("getDataValidation")
    public RestResponse getDataValidation(String versionId){
        try {
            HashMap<Integer, ArrayList<String>> map = tsVulnerabilityService.getDataValidation(versionId);
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get getDataValidation error:{}",e.getMessage(),e);
            return RestResponse.fail("校验失败!");
        }
    }

    /**
     * 计算
     * @param param
     * @return
     */
    @PostMapping("count")
    public RestResponse count(@RequestBody List<TsParam> param){
        try {
            return tsVulnerabilityService.count(param);
        } catch (Exception e) {
            log.error("post count error:{}",e.getMessage(),e);
            return RestResponse.fail("数据异常,计算失败!");
        }
    }

}
